﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Model;
using GtsServer.Manage.Controller;
using GtsServer.Common;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// PrizeAjax 的摘要说明
    /// </summary>
    public class PrizeAjax : IHttpHandler
    {
        PrizeController controller = new PrizeController();
        IISDalHelper helper = new IISDalHelper();
        //存储可作为任务奖励的物品
        static Dictionary<int, string> dict = new Dictionary<int, string>();
        public void ProcessRequest(HttpContext context)
        {
            if (!dict.ContainsKey(0))
                dict[0] = "人民币";
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetTimePropJson":
                        GetTimePropJson(context);
                        break;
                    case "GetPropJson":
                        GetPropJson(context);
                        break;
                    case "GetGoldJson":
                        GetGoldJson(context);
                        break;
                    case "GetListByTypeID":
                        GetListByTypeID(context);
                        break;
                    case "GetCondtionListByTypeID":
                        GetCondtionListByTypeID(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    case "GetListByIsTaskCondition":
                        GetListByIsTaskCondition(context);
                        break;
                    case "GetExchangeTypeJson":
                        GetExchangeTypeJson(context);
                        break;
                    case "GetPrizeName":
                        GetPrizeName(context);
                        break;
                    case "GetPrizeNameByPrizeTypeID":
                        GetPrizeNameByPrizeTypeID(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void GetPrizeNameByPrizeTypeID(HttpContext context)
        {
            int typeID = Convert.ToInt32(context.Request["typeID"]);
            List<Prize> list = controller.GetPrizeName(typeID);
            string json = JsonHelper.ListToJson<Prize>(list);
            context.Response.Write(json);
        }

        private void GetCondtionListByTypeID(HttpContext context)
        {
            int typeID = Convert.ToInt32(context.Request["typeID"].ToString());
            List<Prize> pirzeList = controller.GetList(" (IsShopShow=0 or IsMainGold=1) and prizeTypeID =" + typeID);
            for (int i = 0; i < pirzeList.Count; i++)
            {
                pirzeList[i].Remark = Microsoft.JScript.GlobalObject.encodeURIComponent(pirzeList[i].Remark);
            }
            context.Response.Write(JsonHelper.ListToJson<Prize>(pirzeList));
        }

        private void GetTimePropJson(HttpContext context)
        {
            List<TimeBoxTypeModel> list = new List<TimeBoxTypeModel>();
            foreach (var item in Enum.GetValues(typeof(EnumTimeBoxType)))
            {
                TimeBoxTypeModel model = new TimeBoxTypeModel()
                {
                    Name = item.ToString(),
                    ID = Convert.ToInt32(item)
                };
                list.Add(model);
            }
            context.Response.Write(JsonHelper.ListToJson<TimeBoxTypeModel>(list));
        }

        private void GetPropJson(HttpContext context)
        {
            List<Prize> list = controller.GetListIDNames(" IsMainGold <> 1");
            context.Response.Write(JsonHelper.ListToJson<Prize>(list));
        }

        private void GetGoldJson(HttpContext context)
        {
            List<Prize> list = controller.GetListIDNames(" IsMainGold =1 ");
            context.Response.Write(JsonHelper.ListToJson<Prize>(list));
        }

        private void GetExchangeTypeJson(HttpContext context)
        {
            List<ExchangeType> list = new List<ExchangeType>();
            foreach (var item in dict)
            {
                ExchangeType model = new ExchangeType()
                {
                    ExchangeID = item.Key,
                    ExchangeName = item.Value
                };
                list.Add(model);
            }
            context.Response.Write(JsonHelper.ListToJson<ExchangeType>(list));
        }

        private void GetListByIsTaskCondition(HttpContext context)
        {
            List<PrizeShow> list = controller.GetListByIsTaskCondition();

            foreach (var item in list)
            {
                item.Remark = Microsoft.JScript.GlobalObject.encodeURIComponent(item.Remark);
            }

            context.Response.Write(JsonHelper.ListToJson<PrizeShow>(list));
        }

        private void Delete(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            string imgPath = context.Request["imgPath"];

            if (helper.DeleteContent(imgPath) && dict.ContainsKey(id))
                dict.Remove(id);

            bool isSuccess = false;
            isSuccess = controller.Delete(id);
            if (isSuccess)
            {
                FuncPrizeParmsController funcController = new FuncPrizeParmsController();
                funcController.DeleteByPrizeID(id);
            }
            context.Response.Write(isSuccess);
        }

        private void Update(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            string name = context.Request["name"];
            string imgPath = context.Request["imgPath"];
            int typeID = Convert.ToInt32(context.Request["typeID"].ToString());
            decimal price = Convert.ToDecimal(context.Request["price"].ToString());
            int sortNum = Convert.ToInt32(context.Request["sortNum"].ToString());
            string remark = context.Request["remark"];
            string oldImgPath = context.Request["oldImgPath"];
            bool isTaskCondition = Convert.ToBoolean(context.Request["isTaskCondition"].ToString());
            bool isShopShow = Convert.ToBoolean(context.Request["isShopShow"].ToString());
            int exchangeID = Convert.ToInt32(context.Request["exchangeID"].ToString());
            int prizeCount = Convert.ToInt32(context.Request["prizeCount"].ToString());

            bool isSuccess = false;
            if (!string.IsNullOrEmpty(imgPath))
                isSuccess = helper.SaveContentToService(imgPath, oldImgPath);
            else
            {
                imgPath = oldImgPath;
                isSuccess = true;
            }
            if (isSuccess)
            {
                int lastIndex = imgPath.LastIndexOf("\\") + 1;
                Prize model = new Prize()
                {
                    ID = id,
                    Image = imgPath.Substring(lastIndex, imgPath.Length - lastIndex),
                    Name = name,
                    Price = price,
                    PrizeTypeID = typeID,
                    SortNum = sortNum,
                    Remark = remark,
                    IsTaskCondition = isTaskCondition,
                    IsShopShow = isShopShow,
                    ExchangeType = exchangeID,
                    PrizeCount = prizeCount
                };
                if (controller.Update(model))
                {
                    context.Response.Write(model.ID + "|" + "|" + CommonConfig.imgServiceAddr + CommonConfig.image + model.Image);
                    if (dict.ContainsKey(model.ID) && !model.IsTaskCondition)
                        dict.Remove(model.ID);
                    else if (!dict.ContainsKey(model.ID) && model.IsTaskCondition)
                        dict.Add(model.ID, model.Name);
                    else if (dict.ContainsKey(model.ID) && model.IsTaskCondition)
                        dict[model.ID] = model.Name;
                }
                else
                    context.Response.Write("更新失败！");
            }
        }

        private void GetPrizeName(HttpContext context)
        {
            List<Prize> list = controller.GetPrizeName();
            string json = JsonHelper.ListToJson<Prize>(list);
            context.Response.Write(json);
        }
        private void Add(HttpContext context)
        {
            string name = context.Request["name"];
            string imgPath = context.Request["imgPath"];
            int typeID = Convert.ToInt32(context.Request["typeID"].ToString());
            decimal price = Convert.ToDecimal(context.Request["price"].ToString());
            int sortNum = Convert.ToInt32(context.Request["sortNum"].ToString());
            string remark = context.Request["remark"];
            bool isTaskCondition = Convert.ToBoolean(context.Request["isTaskCondition"].ToString());
            bool isShopShow = Convert.ToBoolean(context.Request["isShopShow"].ToString());
            int exchangeID = Convert.ToInt32(context.Request["exchangeID"].ToString());
            int prizeCount = Convert.ToInt32(context.Request["prizeCount"].ToString());

            bool isSuccess = helper.SaveContentToService(imgPath);
            if (isSuccess)
            {
                int lastIndex = imgPath.LastIndexOf("\\") + 1;
                Prize model = new Prize()
                {
                    Image = imgPath.Substring(lastIndex, imgPath.Length - lastIndex),
                    Name = name,
                    Price = price,
                    CreateTime = DateTime.Now,
                    PrizeTypeID = typeID,
                    SortNum = sortNum,
                    Remark = remark,
                    IsTaskCondition = isTaskCondition,
                    IsShopShow = isShopShow,
                    ExchangeType = exchangeID,
                    PrizeCount = prizeCount
                };
                int id = controller.Add(model);

                if (model.IsTaskCondition)
                    dict.Add(id, model.Name);
                context.Response.Write(id + "|" + model.CreateTime + "|" + CommonConfig.imgServiceAddr + CommonConfig.image + model.Image);
            }
            else
                context.Response.Write("图片上传图片服务器失败！");
        }



        private void GetListByTypeID(HttpContext context)
        {
            int pageIndex = Convert.ToInt32(context.Request["pageIndex"]);
            int pageSize = Convert.ToInt32(context.Request["pageSize"]);
            if (dict.Count == 1)
            {
                List<Prize> pirzeList = controller.GetList(" IsTaskCondition=1 or IsMainGold=1 ");
                for (int i = 0; i < pirzeList.Count; i++)
                {
                    pirzeList[i].Remark = Microsoft.JScript.GlobalObject.encodeURIComponent(pirzeList[i].Remark);
                }
                foreach (var item in pirzeList)
                    dict[item.ID] = item.Name;
            }
            int count = 0;
            int typeID = Convert.ToInt32(context.Request["typeID"].ToString());
            List<PrizeShow> list = controller.GetList(pageIndex, pageSize, out count, typeID);
            foreach (var item in list)
            {
                item.Remark = Microsoft.JScript.GlobalObject.encodeURIComponent(item.Remark);
                item.Image = CommonConfig.imgServiceAddr + CommonConfig.image + item.Image;
            }
            context.Response.Write(JsonHelper.ListToJson<PrizeShow>(list, count.ToString()));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}